package com.xh.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.car.bean.Department;
import com.xh.car.mapper.DepartmentMapper;
import com.xh.car.service.DepartmentService;
import com.xh.car.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/5/7/007
 */
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public R getPage(int page, int limit, Department department) {
        PageHelper.startPage(page, limit);
        List<Department> list = departmentMapper.getAll(department);
        PageInfo<Department> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void saveOrUpdate(Department department) {
        if (department.getId() == null) {
            departmentMapper.insertSelective(department);
        } else {
            departmentMapper.updateByPrimaryKeySelective(department);
        }
    }

    @Override
    public Department getById(int id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(int id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public R getSelect() {
        return R.data(departmentMapper.getAll(new Department()));
    }
}
